<?php

class Yapary_Webservice_Ebay_Ebay {

	private $arrData;
	private $imageBlob		= NULL;
	/* @var $oConnection Yapary_Process_Connection */
	private $oConnection	= NULL;


	public function __construct(array $_arrData) {
		$this->arrData		= $_arrData;
		$this->oConnection	= new Yapary_Process_Connection($this->getUrlImage());
	}


	public function &getConnection () {
		return $this->oConnection;
	}


	public function setImage ($_imageBlob) {
		$this->imageBlob	= $_imageBlob;
	}


	public function getItemId () {
		return (isset($this->arrData['ItemID'])) ? $this->arrData['ItemID'] : NULL;
	}


	public function getTitle () {
		return (isset($this->arrData['Title'])) ? str_replace(array('"', "'", '>', '<', '*', '?'), array('', '', '', '', '', ''), $this->arrData['Title']) : NULL;
	}


	public function getUrl () {
		return (isset($this->arrData['ViewItemURLForNaturalSearch'])) ? $this->arrData['ViewItemURLForNaturalSearch'] : NULL;
	}

	public function imageExists () {
		if ($this->getUrlImage() === NULL) {
			return false;
		}

		return true;
	}

	public function getUrlImage () {
		return (isset($this->arrData['GalleryURL']) && $this->arrData['GalleryURL'] != '') ? $this->arrData['GalleryURL'] : NULL;
	}

	public function getImageBase64 () {
		$image			= $this->getUrlImage();
		if ($image === NULL) { // No image available
			return ''; // empty png file
		}

		if ($this->oConnection->isSucceeded()) { // Asynchron loaded data available. Copy image blob
			$this->imageBlob	= $this->oConnection->getContent();
		}

		if ($this->imageBlob !== NULL) {
			$imageData	= base64_encode($this->imageBlob);
		} else {
			$imageData	= base64_encode(file_get_contents($image));
		}

		return 'data:image/png;base64,'.$imageData;
	}

	public function getImageBlob () {
		$image			= $this->getUrlImage();

		if ($image === NULL) {
			return NULL;
		}

		if ($this->imageBlob !== NULL) {
			return $this->imageBlob;
		}
		return file_get_contents($image);
	}



	public function getEndTime () {
		return (isset($this->arrData['EndTime'])) ? $this->arrData['EndTime'] : NULL;
	}


	public function getTimeLeft () {
		return (isset($this->arrData['TimeLeft'])) ? $this->arrData['TimeLeft'] : NULL;
	}


	public function getListingType () {
		return (isset($this->arrData['ListingType'])) ? $this->arrData['ListingType'] : NULL;
	}


	public function getListingStatus () {
		return (isset($this->arrData['ListingStatus'])) ? $this->arrData['ListingStatus'] : NULL;
	}


	public function getCategoryList () {
		if (!isset($this->arrData['PrimaryCategoryName'])) {
			return array();
		}

		return explode(':',$this->arrData['PrimaryCategoryName']);
	}


	public function getBidCount () {
		return (isset($this->arrData['BidCount'])) ? $this->arrData['BidCount'] : NULL;
	}


	public function getWatchCount () {
		return (isset($this->arrData['WatchCount'])) ? $this->arrData['WatchCount'] : NULL;
	}


	/**
	 * @return Yapary_Webservice_Ebay_Price
	 * */
	public function getPrice () {
		if (!isset($this->arrData['ConvertedCurrentPrice'])) {
			return NULL;
		}

		return new Yapary_Webservice_Ebay_Price($this->arrData['ConvertedCurrentPrice']);
	}


}


?>